﻿using Bll;
using DecorationSys.Models;
using Entity;
using Entity.ViewModel.UserInfo;
using IBll;
using Microsoft.AspNetCore.Mvc;

namespace RepositorySys.Controllers
{
    /// <summary>
    /// 用户相关
    /// </summary>
    public class UserInfoController : Controller
    {
        IUserInfoBll _userInfoBll;

        public UserInfoController(IUserInfoBll userInfoBll)
        {
            _userInfoBll = userInfoBll;
        }

        /// <summary>
        /// 用户基本信息视图
        /// </summary>
        /// <returns></returns>
        public IActionResult UserInfoView()
        {
            return View();
        }

        /// <summary>
        /// 修改密码的视图
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdatePasswordView()
        {
            return View();
        }

        /// <summary>
        /// 用户列表视图
        /// </summary>
        /// <returns></returns>
        public IActionResult UserInfoListView()
        {
            return View();
        }

        /// <summary>
        /// 添加用户视图
        /// </summary>
        /// <returns></returns>
        public IActionResult CreateUserInfoView()
        {
            return View();
        }

        /// <summary>
        /// 修改用户视图
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdateUserInfoView()
        {
            return View();
        }

        /// <summary>
        /// 根据用户id获取用户信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet]
        public MyJsonResult GetUserInfoById(string userId)
        {
            UserInfoViewModel userInfo = _userInfoBll.GetUserInfoById(userId);

            return MyJsonResult.GetSuccessResult(userInfo);
        }

        /// <summary>
        /// 更新用户
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="userName"></param>
        /// <param name="phoneNum"></param>
        /// <param name="email"></param>
        /// <param name="sex"></param>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult UpdateUserInfo(string userId, string userName, string phoneNum, string email, int sex, string departmentId)
        {
            //参数验证
            if (string.IsNullOrEmpty(userId))
            {
                return MyJsonResult.GetErrorResult("用户id不能为空");
            }
            if (string.IsNullOrEmpty(userName))
            {
                return MyJsonResult.GetErrorResult("用户名不能为空");
            }

            //调用业务逻辑层
            bool isSuccess = _userInfoBll.UpdateUserInfo(userId, userName, phoneNum, email, sex, departmentId);
            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult("更新失败");
            }
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="oldPassword"></param>
        /// <param name="newPassword"></param>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult UpdatePassword(string oldPassword, string newPassword)
        {
            //从session中拿到用户id
            string userId = HttpContext.Session.GetString("UserId");

            //参数验证
            if (string.IsNullOrEmpty(userId))
            {
                return MyJsonResult.GetErrorResult("请重新登录");
            }
            if (string.IsNullOrEmpty(oldPassword))
            {
                return MyJsonResult.GetErrorResult("旧密码不能为空");
            }
            if (string.IsNullOrEmpty(newPassword))
            {
                return MyJsonResult.GetErrorResult("新密码不能为空");
            }

            string msg;
            bool isSuccess = _userInfoBll.UpdatePassword(userId, oldPassword, newPassword, out msg);

            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult(msg);
            }
        }

        /// <summary>
        /// 获取用户列表数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public object GetUserInfoList(string userName, string phone, int page, int limit)
        {
            if (page <= 0)
            {
                return new
                {
                    code = 0,
                    msg = "页码有误",
                    count = 0,
                    data = new List<object>()
                };
            }

            if (limit <= 0)
            {
                return new
                {
                    code = 0,
                    msg = "每页条数有误",
                    count = 0,
                    data = new List<object>()
                };
            }

            int count;

            //查用户数据
            List<GetUserInfoListViewModel> list = _userInfoBll.GetUserInfoList(userName, phone, page, limit, out count);

            return new
            {
                code = 0,
                msg = "cg",
                count = count,
                data = list
            };
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult CreateUserInfo(string userName, string account, string phone, int sex, string email, string departmentId)
        {
            //参数验证
            if (string.IsNullOrEmpty(userName))
            {
                return MyJsonResult.GetErrorResult("请输入用户名");
            }
            //参数验证
            if (string.IsNullOrEmpty(account))
            {
                return MyJsonResult.GetErrorResult("请输入账号");
            }
            //创建业务逻辑层
            //UserInfoBll userInfoBll = new UserInfoBll();

            string msg;
            bool isSuccess = _userInfoBll.CreateUserInfo(userName, account, phone, sex, email, departmentId, out msg);

            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult(msg);
            }
        }

        /// <summary>
        /// 软删除
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult DeleteUserInfo(string userId)
        {
            if (string.IsNullOrEmpty(userId))
            {
                return MyJsonResult.GetErrorResult("参数有误");
            }

            bool isSuccess = _userInfoBll.DeleteUserInfo(userId);

            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult("失败");
            }
        }

        /// <summary>
        /// 批量软删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult DeleteUserInfos(List<string> userIds)
        {
            if (userIds.Count <= 0 || userIds == null)
            {
                return MyJsonResult.GetErrorResult("参数有误");
            }

            bool isSuccess = _userInfoBll.DeleteUserInfos(userIds);

            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult("失败");
            }
        }

        /// <summary>
        /// 获取下拉选备选数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public MyJsonResult GetSelectOptionList(string userId)
        {
            //获取下拉选备选数据
            var list = _userInfoBll.GetSelectOptionList();
            return MyJsonResult.GetSuccessResult(list);
        }

    }

}
